package com.mktech.taraservice.http;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.SyncHttpClient;
import com.mktech.taraservice.util.LogTool;

public class HttpTool
{
	private static int DEFAULT_TIME_OUT = 60 * 1000;
	
	private SyncHttpClient mClient;
	private AsyncHttpClient mAsynclient;
	private static HttpTool mInstance;
	private boolean isAsync = false;
	
	public static HttpTool obtain()
	{
		if(mInstance == null)
		{
			mInstance = new HttpTool();
		}
		return mInstance;
	}
	
	private HttpTool()
	{
		mClient = new SyncHttpClient();
		mClient.setTimeout(DEFAULT_TIME_OUT);
		mClient.setConnectTimeout(DEFAULT_TIME_OUT);
		mClient.setResponseTimeout(DEFAULT_TIME_OUT);
		
		mAsynclient = new AsyncHttpClient();
		mAsynclient.setTimeout(DEFAULT_TIME_OUT);
		mAsynclient.setConnectTimeout(DEFAULT_TIME_OUT);
		mAsynclient.setResponseTimeout(DEFAULT_TIME_OUT);
	}
	
	public void get(String url, AsyncHttpResponseHandler res)
	{
		get(url, null, res);
	}
	
	public void get(String url, RequestParams params, AsyncHttpResponseHandler res)
	{
		get(url, params, res, false);
	}
	
	public void get(String url, RequestParams params, AsyncHttpResponseHandler res, boolean isAsync)
	{
		LogTool.i("request: = "+AsyncHttpClient.getUrlWithQueryString(false, url, params));
		if(isAsync)
		{
			mAsynclient.get(url, params, res);
		}
		else
		{
			mClient.get(url, params, res);
		}
	}
	
	public void post(String url, RequestParams params, AsyncHttpResponseHandler res)
	{
		post(url, params, res, false);
	}
	public void post(String url, RequestParams params, AsyncHttpResponseHandler res, boolean isAsync)
	{
		if(isAsync)
		{
			mAsynclient.post(url, params, res);
		}
		else
		{
			mClient.post(url, params, res);
		}
	}
	
}
